package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IContractRepository;
import cn.tedu.antconlonysever.contract.dao.persist.repository.ILossRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Loss;
import cn.tedu.antconlonysever.contract.pojo.param.LossAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.LossUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractStandardVO;
import cn.tedu.antconlonysever.contract.pojo.vo.LossListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.LossStandardVO;
import cn.tedu.antconlonysever.contract.service.ILossService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
@Slf4j
public class LossServiceImpl implements ILossService {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ILossRepository lossRepository;
    @Autowired
    private IContractRepository contractRepository;
    @Override

    public void addNew(LossAddNewParam lossAddNewParam) {

        log.debug("开始处理【添加流失客户】的业务，参数：{}", lossAddNewParam);
        String cusNo = lossAddNewParam.getCusNo();
        int countCusNo = lossRepository.countCusNo(cusNo);
        if(countCusNo>0){
            String message = "添加流失客户失败，流失客户编号已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Loss loss=new Loss();
        BeanUtils.copyProperties(lossAddNewParam,loss);
        loss.setGmtCreate(LocalDateTime.now());
        loss.setGmtModified(LocalDateTime.now());
        int rows = lossRepository.insert(loss);
        if(rows!=1){
            String message = "添加流失客户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除流失客户】的业务，参数：{}", id);
        LossStandardVO standardById = lossRepository.getStandardById(id);
        if(standardById==null){
            String message = "删除流失客户失败，流失客户ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = lossRepository.deleteById(id);
        if(rows!=1){
            String message = "删除流失客户失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

    @Override
    public void updateInfoById(long id, LossUpdateParam lossUpdateParam) {
        log.debug("开始处理【修改流失客户】的业务，ID：{}，新数据：{}", id, lossUpdateParam);
        LossStandardVO standardById = lossRepository.getStandardById(id);
        if(standardById==null){
            String message = "修改流失客户失败，流失客户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int countCusNoNotId = lossRepository.countCusNoNotId(id, lossUpdateParam.getCusNo());
        if(countCusNoNotId>0){
            String message = "修改流失客户信息失败，尝试修改的流失客户编号已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Loss loss=new Loss();
        BeanUtils.copyProperties(lossUpdateParam,loss);
        loss.setId(id);
        int rows = lossRepository.update(loss);
        if(rows!=1){
            String message = "修改流失客户信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }


    }

    @Override
    public LossStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询流失客户详情】的业务，ID：{}", id);
        LossStandardVO queryResult = lossRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询流失客户详情失败，流失客户数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<LossListVO> list(Integer pageNum) {
        PageData<LossListVO> pageData = lossRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<LossListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询流失客户列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<LossListVO> pageData = lossRepository.list(pageNum, pageSize);
        return pageData;
    }


}
